//
//  CSTimeRangeView.m
//  市场监管通
//
//  Created by 网新中研 on 16/5/9.
//  Copyright © 2016年 centralsoft. All rights reserved.
//

#import "CSTimeRangeView.h"
#import "CSDatePickerView.h"
#import "Common.h"
#import "CSPageElementModel.h"

@interface CSTimeRangeView()

@property (nonatomic, strong) UIViewController *superViewController;

@property (nonatomic, strong) UITextField *startTimeTextField;
@property (nonatomic, strong) UITextField *endTimeTextField;
@property (nonatomic, strong) UIButton *startTimeButton;
@property (nonatomic, strong) UIButton *endTimeButton;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UIView *seperatorLineView;

/**
 *  开始提示
 */
@property (nonatomic, strong) NSString *startTimePlaceHolder;
/**
 *  结束提示
 */
@property (nonatomic, strong) NSString *endTimePlaceHolder;

@end

@implementation CSTimeRangeView

#define CENTER_LEABEL_WIDHT 42.0

#define PLACE_HOLDER_START_TIME @"开始时间"
#define PLACE_HOLDER_END_TIME @"结束时间"

#define LEFT_DEVIATION 30.0//“至”字左侧平移

-(NSInteger)viewType{
    return CSViewType_PageTimePickView;
}

+(id)timeRangeView:(UIViewController *)superViewController{
    CSTimeRangeView *timerRangeView = [CSTimeRangeView new];
    timerRangeView.superViewController = superViewController;
    return timerRangeView;
}

+(id)timeRangeView:(UIViewController *)superViewController startTimePlaceHolder:(NSString *)startTimePlaceHolder endTimePlaceHolder:(NSString *)endTimePlaceHolder{
    CSTimeRangeView *timerRangeView = [CSTimeRangeView new];
    timerRangeView.superViewController = superViewController;
    timerRangeView.startTimePlaceHolder = startTimePlaceHolder;
    timerRangeView.endTimePlaceHolder = endTimePlaceHolder;
    return timerRangeView;
}

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    //至
    self.label.frame =  CGRectMake((kWindowW - CENTER_LEABEL_WIDHT)/2 - LEFT_DEVIATION, 0, CENTER_LEABEL_WIDHT, CELL_LABLE_HEIGHT + 2 * QUERY_PANEL_BUTTON_MARGIN);
    [self addSubview:self.label];
    
    //开始时间
    self.startTimeTextField.frame = CGRectMake(CELL_SEPERATOR_MARGIN_LEFT, 0, (kWindowW - CENTER_LEABEL_WIDHT) / 2.0 - CELL_SEPERATOR_MARGIN_LEFT- LEFT_DEVIATION, CELL_LABLE_HEIGHT + 2 * QUERY_PANEL_BUTTON_MARGIN);
    self.startTimeButton.frame = CGRectMake(0, 0, (kWindowW - CENTER_LEABEL_WIDHT) / 2.0 - LEFT_DEVIATION, CELL_LABLE_HEIGHT + 2 * QUERY_PANEL_BUTTON_MARGIN);
    [self addSubview:self.startTimeTextField];
    [self addSubview:self.startTimeButton];
    
    //结束时间
    self.endTimeTextField.frame = CGRectMake((kWindowW + CENTER_LEABEL_WIDHT)/2, 0, (kWindowW - CENTER_LEABEL_WIDHT) / 2.0, CELL_LABLE_HEIGHT + 2 * QUERY_PANEL_BUTTON_MARGIN);
    self.endTimeButton.frame = CGRectMake((kWindowW + CENTER_LEABEL_WIDHT)/2, 0, (kWindowW - CENTER_LEABEL_WIDHT) / 2.0, CELL_LABLE_HEIGHT + 2 * QUERY_PANEL_BUTTON_MARGIN);
    [self addSubview:self.endTimeTextField];
    [self addSubview:self.endTimeButton];
    
    //分割线
    self.seperatorLineView.frame = CGRectMake(CELL_SEPERATOR_MARGIN_LEFT, 0 + CELL_LABLE_HEIGHT + 2 * QUERY_PANEL_BUTTON_MARGIN, kWindowW - CELL_SEPERATOR_MARGIN_LEFT, CELL_SEPERATOR_LINE_HEIGHT);
    [self addSubview:self.seperatorLineView];
    
    [superView addSubview:self];
    
    self.frame = CGRectMake(0, primaryY, kWindowW, + CELL_LABLE_HEIGHT + 2 * QUERY_PANEL_BUTTON_MARGIN + CELL_SEPERATOR_LINE_HEIGHT);
    
    return primaryY + CELL_LABLE_HEIGHT + 2 * QUERY_PANEL_BUTTON_MARGIN + CELL_SEPERATOR_LINE_HEIGHT;
}



- (void)chooseStartTimeClick{
    
    if (self.superViewController == nil) {
        return;
    }
    WS(weakSelf);
    CSDatePickerView *dateTimePickerView = [CSDatePickerView instanceDateTimePickViewWithSaveClickHandler:^(NSString *selectDate) {
        weakSelf.startTimeTextField.text = selectDate;
    } cancelClickHandler:nil];
    
    [dateTimePickerView popShow:self.superViewController];
}

-(void)reset{
    self.startTimeTextField.text = @"";
    self.endTimeTextField.text = @"";
}



- (void)chooseEndTimeClick{
    if (self.superViewController == nil) {
        return;
    }
    
    WS(weakSelf);
    CSDatePickerView *dateTimePickerView = [CSDatePickerView instanceDateTimePickViewWithSaveClickHandler:^(NSString *selectDate) {
        weakSelf.endTimeTextField.text = selectDate;
    } cancelClickHandler:nil];
    
    [dateTimePickerView popShow:self.superViewController];
}

- (UIView *)seperatorLineView {
	if(_seperatorLineView == nil) {
		_seperatorLineView = [[UIView alloc] init];
        STYLE_SEPERATOR_LINE(_seperatorLineView);
	}
	return _seperatorLineView;
}

- (UITextField *)startTimeTextField {
	if(_startTimeTextField == nil) {
		_startTimeTextField = [[UITextField alloc] init];
        _startTimeTextField.placeholder = self.startTimePlaceHolder == nil? PLACE_HOLDER_START_TIME : self.startTimePlaceHolder;
        _startTimeTextField.font = [UIFont systemFontOfSize:15.0];
        _startTimeTextField.textAlignment = NSTextAlignmentLeft;
	}
	return _startTimeTextField;
}

- (UITextField *)endTimeTextField {
	if(_endTimeTextField == nil) {
		_endTimeTextField = [[UITextField alloc] init];
        _endTimeTextField.placeholder = self.endTimePlaceHolder == nil? PLACE_HOLDER_END_TIME : self.endTimePlaceHolder; 
        _endTimeTextField.font = [UIFont systemFontOfSize:15.0];
        _endTimeTextField.textAlignment = NSTextAlignmentLeft;
	}
	return _endTimeTextField;
}

- (UIButton *)startTimeButton {
	if(_startTimeButton == nil) {
		_startTimeButton = [[UIButton alloc] init];
        [_startTimeButton addTarget:self action:@selector(chooseStartTimeClick) forControlEvents:UIControlEventTouchUpInside];
	}
	return _startTimeButton;
}

- (UIButton *)endTimeButton {
	if(_endTimeButton == nil) {
		_endTimeButton = [[UIButton alloc] init];
        [_endTimeButton addTarget:self action:@selector(chooseEndTimeClick) forControlEvents:UIControlEventTouchUpInside];
	}
	return _endTimeButton;
}

- (UILabel *)label {
	if(_label == nil) {
		_label = [[UILabel alloc] init];
        _label.text = @"至";
        _label.textAlignment = NSTextAlignmentCenter;
        _label.font = [UIFont systemFontOfSize:16.0];
	}
	return _label;
}

-(NSString *)startTime{
    return _startTimeTextField.text;
}

-(NSString *)endTime{
    return _endTimeTextField.text;
}

@end
